//
//  NewsViewController.m
//  YiLiao
//
//  Created by Infinite on 16/7/1.
//  Copyright © 2016年 Infinite. All rights reserved.
//

#import "NewsViewController.h"
#import <AFNetworking.h>
#import "NewsTableViewCell.h"
#import "NewS2TableViewCell.h"
#import "DetialNewsViewController.h"




@interface NewsViewController ()<UITableViewDataSource,UITableViewDelegate>{
 __strong   NSDictionary * _allNewsDic;
 __strong   NSArray * _allArray;
}

@property (nonatomic,strong) UITableView *tableView;

@end

@implementation NewsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after lo	ading the view from its nib.
        self.view.backgroundColor=[UIColor grayColor];
    self.title=@"新闻中心";
    [self AFNet];
    
    self.tableView =[[UITableView alloc]initWithFrame:CGRectMake(0,-32, ScreenWidth, ScreenHeight) style:(UITableViewStyleGrouped)];
    _tableView.dataSource=self;
    _tableView.delegate=self;
    _tableView.rowHeight=100;
    [_tableView registerNib:[UINib nibWithNibName:@"NewsTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"cell1"];
    [_tableView registerNib:[UINib nibWithNibName:@"NewS2TableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"cell2"];
    [self.view addSubview:self.tableView];
    
    
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _allArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary * allPresonDic = [_allArray objectAtIndex:indexPath.row];
    NSArray * imageArray = [allPresonDic objectForKey:@"images"];
    if (imageArray.count==1 || imageArray.count==2) {
        NewsTableViewCell * cell1 = [tableView dequeueReusableCellWithIdentifier:@"cell1" forIndexPath:indexPath];
        NSDictionary *iamgeDic = [imageArray objectAtIndex:0];
        cell1.headImage.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://localhost:8080%@",[iamgeDic objectForKey:@"url"]]]]];
        cell1.titleLabel.text=[allPresonDic objectForKey:@"news_title"];
        cell1.deTextLabel.text=[allPresonDic objectForKey:@"intro"];
        [cell1.button setTitle:[allPresonDic objectForKey:@"source"] forState:(UIControlStateNormal)];
        
        
        return cell1;
    }else{
        NewS2TableViewCell * cell2 = [tableView dequeueReusableCellWithIdentifier:@"cell2" forIndexPath:indexPath];
        cell2.titleTextLabel.text=[allPresonDic objectForKey:@"news_title"];
        [cell2.button setTitle:[allPresonDic objectForKey:@"source"] forState:(UIControlStateNormal)];
        NSDictionary * imageDic = [imageArray objectAtIndex:0];
        NSDictionary * imageDic1 = [imageArray objectAtIndex:1];
        NSDictionary * imageDic2 = [imageArray objectAtIndex:2];
        cell2.image1.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://localhost:8080%@",[imageDic objectForKey:@"url"]]]]];
        cell2.image2.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://localhost:8080%@",[imageDic1 objectForKey:@"url"]]]]];
        cell2.iamge3.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://localhost:8080%@",[imageDic2 objectForKey:@"url"]]]]];
        
        return cell2;
    }
    
   
    
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary * allPresonDic = [_allArray objectAtIndex:indexPath.row];
    NSString * SourceUrl = [allPresonDic objectForKey:@"source_url"];
    
    DetialNewsViewController * DNVC = [[DetialNewsViewController alloc]init];
    DNVC.Sourceurl=SourceUrl;
    [self.navigationController pushViewController:DNVC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)AFNet{
    AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
    
    manager.responseSerializer.acceptableContentTypes=[manager.responseSerializer.acceptableContentTypes setByAddingObjectsFromSet:[NSSet setWithObjects:@"text/plain",nil]];
    
    [manager GET:@"http://localhost:8080/st/news/news_list.json" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        _allNewsDic=(NSDictionary *)responseObject;
        _allArray= [_allNewsDic objectForKey:@"news_list"];

         
         [self.tableView reloadData];

       
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
         NSLog(@"%@",error);
    }];
    
    

}




/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
